//
//  HMAnimatedTransitioning.m
//  05-自定义UIPresentationController
//
//  Created by 冯华强 on 15/10/19.
//  Copyright (c) 2015年 HQ. All rights reserved.
//

#import "HMAnimatedTransitioning.h"
#import "UIView+Extension.h"

@implementation HMAnimatedTransitioning

NSTimeInterval duration = 0.5;
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return duration;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    if (self.presented){
        UIView *containerView = [transitionContext containerView];
        UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
        toView.x = containerView.centerX - toView.width * 0.5;
        toView.y = containerView.height;
        [UIView animateWithDuration:duration animations:^{
            toView.y = containerView.centerY - toView.height * 0.5;
        } completion:^(BOOL finished) {
            //动画执行完毕要通知，不然modal出来的页面接收不了事件
            [transitionContext completeTransition:YES];
        }];
    }else{
        UIView *containerView = [transitionContext containerView];
        UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        [UIView animateWithDuration:duration animations:^{
            fromView.y = containerView.height;
        } completion:^(BOOL finished) {
            //动画执行完毕要通知，不然modal出来的页面接收不了事件
            [transitionContext completeTransition:YES];
        }];
    }
}

@end
